package com.twh.www.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.twh.www.entity.WnGoods;
import com.twh.www.mapper.WnGoodsMapper;
import com.twh.www.service.WnGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.twh.www.service.RedisService;

import java.util.List;

/**
 * @author 谭万豪
 * @description 针对表【wn_goods】的数据库操作Service实现
 * @createDate 2025-04-25 22:09:06
 */
@Service
public class WnGoodsServiceImpl extends ServiceImpl<WnGoodsMapper, WnGoods>
        implements WnGoodsService {

    @Autowired
    private RedisService redisService;

    // 查询所有商品
    @Override
    public List<WnGoods> getAllGoods() {
        return this.list();
    }

    // 分页查询所有商品
    @Override
    public Page<WnGoods> getGoodsByPage(int pageNum, int pageSize) {
        // 先查缓存
        List<?> cachedData = redisService.getGoodsPage(pageNum);
        Long total = redisService.getTotal();

        if (cachedData != null && total != null) {
            System.out.println("使用缓存：第" + pageNum + "页数据");
            Page<WnGoods> page = new Page<>(pageNum, pageSize);
            page.setRecords((List<WnGoods>) cachedData);
            page.setTotal(total);
            return page;
        }

        // 缓存没有，查数据库
        Page<WnGoods> page = new Page<>(pageNum, pageSize);
        this.page(page);

        // 存入缓存
        redisService.cacheGoodsPage(pageNum, page.getRecords());
        redisService.cacheTotal(page.getTotal());

        return page;
    }

    // 模糊查询商品并分页
    @Override
    public Page<WnGoods> searchGoodsByPage(String keyword, int pageNum, int pageSize) {
        // 构建查询条件
        QueryWrapper<WnGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", keyword); // 假设商品名称字段为 name

        // 先查缓存
        String cacheKey = "goods:search:" + keyword + ":" + pageNum;
        List<?> cachedData = (List<?>) redisService.redisTemplate.opsForValue().get(cacheKey);
        Long total = redisService.getTotal();

//        System.out.println(cachedData);
        if (cachedData != null && total != null) {
            System.out.println("使用缓存：搜索关键词'" + keyword + "'的第" + pageNum + "页数据");
            Page<WnGoods> page = new Page<>(pageNum, pageSize);
            page.setRecords((List<WnGoods>) cachedData);
            page.setTotal(total);
            return page;
        }

        // 缓存没有，查数据库
        Page<WnGoods> page = new Page<>(pageNum, pageSize);
//        System.out.println("查数据库");
        this.page(page, queryWrapper);

        // 存入缓存
        redisService.cacheSearchGoodsPage(keyword,pageNum, page.getRecords());
        redisService.cacheTotal(page.getTotal());

        return page;
    }

    // 查询热门商品
    @Override
    public List<WnGoods> getHotGoods() {
        return this.lambdaQuery().eq(WnGoods::getHottest, "Y").list();
    }

    @Override
    public boolean save(WnGoods goods) {
        boolean result = super.save(goods);
        if (result) {
            redisService.clearGoodsCache();
        }
        return result;
    }
}